package com.example.lib_mvp.apkupload;

import android.content.Context;


import com.example.lib_mvp.httpUtil.HttpUrl;
import com.tjf.lib_core.upload.ShowApkDialogS;
import com.tjf.lib_network.HttpHelper;
import com.tjf.lib_network.httpHelper.CallBackUtil;
import com.tjf.lib_utils.GsonUtils;
import com.tjf.lib_utils.LogsUtils;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;


/**
 *
 */
public class CheckApkUtil {
    Context mContext;

    public CheckApkUtil(Context mContext) {
        this.mContext = mContext;
    }

    public void getVersion(OnUpLoadListener onUpLoadListener,String version) {
//        https://cow-gen.uni-sheep.com/api/app-version
        String url = HttpUrl.BASE_URL + "config/version";
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("version", version);
        paramsMap.put("app_type", 3 + "");
        if (onUpLoadListener != null) {
            onUpLoadListener.onStart();
        }
        HttpHelper.getHttpHelper().okHttpGet(url, paramsMap, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(Call call, Exception e) {
                if (onUpLoadListener != null) {
                    onUpLoadListener.onFail(e);
                }
            }

            @Override
            public void onResponse(Call call, String response) {
                LogsUtils.i("getVersion", "response==" + response);
                ApKInfoEntity apKInfoEntity = GsonUtils.fromJson(response, ApKInfoEntity.class);
                if (apKInfoEntity == null) {
                    return;
                }
                if (apKInfoEntity.getCode() == 200) {
                    ApKInfoEntity.DataDTO data = apKInfoEntity.getData();
                    if (data.getIs_update() == 0) {


                    }
                    if (data.getIs_update() == 1) {
                        showDialog(data.getVersion(), data.getContent(),
                                data.getDown_url(), true);

                    }
                   /* if (data.getIs_update() == 2) {

                        showDialog(data.getVersion(), data.getContent(),
                                data.getDown_url(), true);
                    }*/
                    if (onUpLoadListener != null) {
                        onUpLoadListener.onSucceed(data.getIs_update(), apKInfoEntity.getMsg());
                    }
                }
            }
        });
    }


    public void showDialog(String versionName, String content,
                           String url, boolean isMust) {
        new ShowApkDialogS().showDialog(mContext, versionName, content, url, isMust);
    }

    public interface OnUpLoadListener {
        /**
         * 请求开始
         */
        void onStart();

        /**
         * 失败
         */
        void onFail(Exception e);

        /**
         * 成功
         */
        void onSucceed(int is_type, String msg);
    }
}
